package com.ruoyi.pay.refund.zfb.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.ruoyi.config.mapper.ZfbpayConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 🤙🤙🏼🤙🏽
 * @date 2025 4月 17 10:58
 * @Description
 */
@Slf4j
@Component
public class ZfbTransferUtil {

    @Resource
    private ZfbpayConfigMapper zfbpayConfigMapper;

    private AlipayConfig getAlipayConfig() {
        String appPrivateKey = zfbpayConfigMapper.selectOneData("AppPrivateKey");
        String appId = zfbpayConfigMapper.selectOneData("AppId");
        String merchantCertPath = zfbpayConfigMapper.selectOneData("merchantCertPath");
        String alipayCertPath = zfbpayConfigMapper.selectOneData("alipayCertPath");
        String alipayRootCertPath = zfbpayConfigMapper.selectOneData("alipayRootCertPath");
        String privateKey = appPrivateKey;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
        //应用公钥证书
        alipayConfig.setAppCertPath("/home/ruoyi/uploadPath/appCertPublicKey_2021005164639182.crt");
        //支付宝公钥证书
        alipayConfig.setAlipayPublicCertPath("/home/ruoyi/uploadPath/alipayCertPublicKey_RSA2.crt");
        //根证书
        alipayConfig.setRootCertPath("/home/ruoyi/uploadPath/alipayRootCert.crt");
        return alipayConfig;
    }

    public String zfbTransfer(ZfbTransferVo transferVo) throws AlipayApiException {

        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
        model.setOutBizNo(transferVo.getOutBizNo());

        // 设置订单总金额
        model.setTransAmount(transferVo.getTransAmount());

        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");

        // 设置业务产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置转账业务的标题
        model.setOrderTitle(transferVo.getOrderTitle());


        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setIdentity(transferVo.getIdentity());
        payeeInfo.setName(transferVo.getName());
        model.setPayeeInfo(payeeInfo);

        // 设置业务备注
        model.setRemark(transferVo.getOrderTitle());

        // 设置转账业务请求的扩展参数
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        log.info("支付宝提现结果:{}",response.getBody());
        if (response.isSuccess()) {
            log.info("支付宝提现成功:{}",response.getMsg());
        } else {
            throw new RuntimeException("支付宝提现失败:"+response.getSubMsg());
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response.getStatus();
    }
}
